home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Cache / Container / msession.php < prev    next >
PHP Script  |  2004-10-01  |  8KB  |  241 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de>                           |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: msession.php,v 1.4 2003/01/04 11:54:46 mj Exp $
  19.  
  20. require_once 'Cache/Container.php';
  21.  
  22. /**
  23. * Stores cache contents in msessions.
  24. *
  25. * WARNING: experimental, untested
  26. *
  27. * @author   Ulf Wendel  <ulf.wendel@phpdoc.de>
  28. * @version  $Id: msession.php,v 1.4 2003/01/04 11:54:46 mj Exp $
  29. */
  30. class Cache_Container_msession extends Cache_Container {
  31.  
  32.  
  33.     /**
  34.     * Length of the Cache-Identifier
  35.     *
  36.     * Note that the PEAR-Cache prefixes the ID with an md5() value
  37.     * of the cache-group. A good value for the id_length
  38.     * depends on the maximum number of entries per cache group.
  39.     *
  40.     * @var  int
  41.     */
  42.     var $id_length = 32;
  43.     
  44.     
  45.     /**
  46.     * Use msession_uniq to create a unique SID.
  47.     * 
  48.     * @var  boolean
  49.     */
  50.     var $uniq = true;
  51.     
  52.     
  53.     /**
  54.     * Establish a connection to a msession server?
  55.     *
  56.     * @var  boolean
  57.     */
  58.     var $connect = true;
  59.    
  60.    
  61.     /**
  62.     * msession host
  63.     *
  64.     * @var  string
  65.     */  
  66.     var $host = NULL;
  67.     
  68.    
  69.     /**
  70.     * msession port
  71.     *
  72.     * @var  string
  73.     */
  74.     var $port = NULL;
  75.     
  76.     
  77.     /**
  78.     * mesession server connection
  79.     *
  80.     * @var  resource msession
  81.     */
  82.     var $ms = NULL;
  83.  
  84.     
  85.     function Cache_Container_msession($options = '') {
  86.         if (is_array($options))
  87.             $this->setOptions($options, array_merge($this->allowed_options, array('id_lenght', 'uniq', 'host', 'port', 'connect')));
  88.  
  89.         if ($connect) {            
  90.             if (NULL == $this->host)
  91.                 new Cache_Error('No host specified.', __FILE__, __LINE__);
  92.             if (NULL == $this->port)
  93.                 new Cache_Error('No port specified.', __FILE__, __LINE__);
  94.         
  95.             if (!($this->ms = msession_connect($this->host, $this->port)))
  96.                 new Cache_Error('Can not connect to the sever using host "' . $this->host . '" on port "' . $this->port . '"', __FILE__, __LINE__);
  97.         }
  98.         
  99.     } // end func contructor
  100.  
  101.     function fetch($id, $group) {
  102.     
  103.         $id = strtoupper(md5($group)) . $id;
  104.         $group = msession_get($id, '_pear_cache_data', NULL);
  105.         
  106.         if (NULL == $data)
  107.             return array(NULL, NULL, NULL);
  108.         
  109.         return array($data['expires'], $data['cachedata'], $data['userdata']);
  110.     } // end func fetch
  111.  
  112.     /**
  113.     * Stores a dataset.
  114.     *
  115.     * WARNING: If you supply userdata it must not contain any linebreaks,
  116.     * otherwise it will break the filestructure.
  117.     */
  118.     function save($id, $cachedata, $expires, $group, $userdata) {
  119.         $this->flushPreload($id, $group);
  120.         
  121.         $cachedata      = $this->encode($cachedata);
  122.         $expires_abs    = $this->getExpiresAbsolute($expires);
  123.  
  124.         $size = 1 + strlen($cachedata) + strlen($expires_abs) + strlen($userdata) + strlen($group);
  125.         $size += strlen($size);
  126.         
  127.         $data = array(
  128.                     'cachedata' => $cachedata, 
  129.                     'expires'   => $expires_abs,
  130.                     'userdata'  => $userdata
  131.                   );
  132.         $id = strtoupper(md5($group)) . $id;
  133.                             
  134.         msession_lock($id);
  135.         
  136.         if (!msession_set($id, '_pear_cache', true)) {
  137.             msession_unlock($id);
  138.             return new Cache_Error("Can't write cache data.", __FILE__, __LINE__);
  139.         }
  140.         
  141.         if (!msession_set($id, '_pear_cache_data', $data)) {
  142.             msession_unlock($id);
  143.             return new Cache_Error("Can't write cache data.", __FILE__, __LINE__);
  144.         }
  145.         
  146.         if (!msession_set($id, '_pear_cache_group', $group)) {
  147.             msession_unlock($id);
  148.             return new Cache_Error("Can't write cache data.", __FILE__, __LINE__);
  149.         }
  150.         
  151.         if (!msession_set($id, '_pear_cache_size', $size)) {
  152.             msession_unlock($id);
  153.             return new Cache_Error("Can't write cache data.", __FILE__, __LINE__);
  154.         }
  155.         
  156.         // let msession do some GC as well
  157.         // note that msession works different from the PEAR Cache.
  158.         // msession deletes an entry if it has not been used for n-seconds.
  159.         // PEAR Cache deletes after n-seconds.
  160.         if (0 != $expires)
  161.             msession_timeout($id, $expires);
  162.             
  163.         msession_unlock($id);
  164.  
  165.         return true;
  166.     } // end func save
  167.  
  168.     function remove($id, $group) {
  169.         $this->flushPreload($id, $group);
  170.     
  171.         return msession_destroy(strtoupper(md5($group)) . $id);
  172.     } // end func remove
  173.  
  174.     function flush($group) {
  175.         $this->flushPreload();
  176.       
  177.         $sessions = msession_find('_pear_cache_group', $group);
  178.         if (empty($sessions))
  179.             return 0;
  180.         
  181.         foreach ($sessions as $k => $id)
  182.             messsion_destroy($id);
  183.  
  184.         return count($sessions);
  185.     } // end func flush
  186.  
  187.     function idExists($id, $group) {
  188.         
  189.         return (NULL == msession_get(strtoupper(md5($group)) . $id, '_pear_cache_group', NULL)) ? false : true;
  190.     } // end func idExists
  191.  
  192.     /**
  193.     * Deletes all expired files.
  194.     *
  195.     * Note: garbage collection should cause lot's of network traffic.
  196.     *
  197.     * @param    integer Maximum lifetime in seconds of an no longer used/touched entry
  198.     * @throws   Cache_Error
  199.     */
  200.     function garbageCollection($maxlifetime) {
  201.         $this->flushPreload();
  202.         
  203.         $sessions = msession_find('_pear_cache', true);
  204.         if (empty($sessions))
  205.             return true;
  206.         
  207.         $total = 0;
  208.         $entries = array();
  209.         
  210.         foreach ($sessions as $k => $id) {
  211.             $data = msession_get($id, '_pear_cache_data', NULL);
  212.             if (NULL == $data)
  213.                 continue;
  214.                 
  215.             if ($data['expires'] <= time()) {
  216.                 msession_destroy($id);
  217.                 continue;
  218.             }
  219.             
  220.             $size = msession_get($id, '_pear_cache_size', NULL);
  221.             $total += $size;
  222.             $entries[$data['expires']] = array($id, $size);
  223.         }
  224.         
  225.         if ($total > $this->highwater) {
  226.             
  227.             krsort($entries);
  228.             reset($entries);
  229.             
  230.             while ($total > $this->lowwater && list($expires, $entry) = each($entries)) {
  231.                 msession_destroy($entry[0]);
  232.                 $total -= $entry[1];
  233.             }
  234.             
  235.         }
  236.         
  237.         return true;
  238.     } // end func garbageCollection
  239.     
  240. } // end class file
  241. ?>